package com.example.test.fragment;

import java.util.Random;

import com.example.test.ActivityControler;
import com.example.test.MainActivity;
import com.example.test.R;
import com.example.test.RegActivity;
import com.example.test.utils.DbDaoUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class RegFragment extends Fragment implements OnClickListener {

	private Context ct;
	@ViewInject(R.id.et_phone)
	private EditText et_phone;
	@ViewInject(R.id.bt_reg)
	private Button btn_reg;
	private String uername;
	private String password;
	private String phone;
	@ViewInject(R.id.reg_loading)
	private RelativeLayout reg_loading;
	private SharedPreferences sp;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		ct=getActivity();
	}
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View view = inflater.inflate(R.layout.reg_layout, null);
		ViewUtils.inject(this, view);
		init();
		return view;
	}

	/**
	 * 
	 */
	private void init() {
		
		btn_reg.setOnClickListener(this);		
		reg_loading.setVisibility(View.INVISIBLE);

	}
	private boolean checkForm() {
		// TODO Auto-generated method stub
		
		phone = et_phone.getEditableText().toString().trim();
		
       if(TextUtils.isEmpty(phone)){
    	   Toast.makeText(ct,"电话号码不能为空", Toast.LENGTH_SHORT).show();
    	   return false;
       }
		
		return true;
	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.bt_reg:		
			if(checkForm()){
				reg_loading.setVisibility(View.VISIBLE);			
				uername=phone;				
				password="123456";
				DbDaoUtils daoUtils = new DbDaoUtils(ct);
				long user = daoUtils
						.insertUser(uername, password, phone, 0);
				if (user > 0) {
					Toast.makeText(ct, "注册成功", 0).show();
					reg_loading.setVisibility(View.GONE);
					sp = ct.getSharedPreferences("user", Context.MODE_PRIVATE);
					Editor editor = sp.edit();
					editor.putString("username", uername);
					editor.commit();
					Intent intent=new Intent(ct, MainActivity.class);					
					ActivityControler.finishAll();
					startActivity(intent);					
				} else {
					Toast.makeText(ct, "注册失败", 0).show();
					reg_loading.setVisibility(View.GONE);
				}
			
			}
			break;

		default:
			break;
		}
	}

}
